# encoding: utf-8

from cElementTree import fromstring

from gcapi._root import _Root

class Calendar(_Root):
    
    def __init__(self):
        self._id = None
        self._published = None
        self._updated = None
        self._title = None
        self._link_alternate = None
        self._link_self = None
        self._timezone = None
    
    @classmethod
    def createFromText(cls, text): #@UnusedVariable
        calendars = []
        tree = fromstring(text)
        
        calendarTag = "{%s}entry" % (Calendar._ATOM_SCHEMA_URI,)
        calendarNodes = tree.findall(calendarTag)
        
        for calendarNode in calendarNodes:
            calendar = Calendar()

            calendar.applyId(calendarNode)
            calendar.applyPublished(calendarNode)
            calendar.applyUpdated(calendarNode)
            calendar.applyTitle(calendarNode)
            calendar.applyLinks(calendarNode)
            calendar.applyTimeZone(calendarNode)
            
            calendars.append(calendar)
        
        return calendars
    
    
    def getId(self):
        return self._id
        
    def setId(self, id):
        self._id = id
    
    id = property(getId, setId)
    
    def getPublished(self):
        return self._published
    
    def setPublished(self, published):
        self._published = published
        
    published = property(getPublished, setPublished)
    
    def getUpdated(self):
        return self._updated
    
    def setUpdated(self, updated):
        self._updated = updated
    
    updated = property(getUpdated, setUpdated)
    
    def getTitle(self):
        return self._title
    
    def setTitle(self, title):
        self._title = title
        
    title = property(getTitle, setTitle)
    
    def getLinkAlternate(self):
        return self._link_alternate
    
    def setLinkAlternate(self, linkAlternate):
        self._link_alternate = linkAlternate
        
    linkAlternate = property(getLinkAlternate, setLinkAlternate)
    
    def getLinkSelf(self):
        return self._link_self
    
    def setLinkSelf(self, linkSelf):
        self._link_self = linkSelf
        
    linkSelf = property(getLinkSelf, setLinkSelf)
    
    def getTimezone(self):
        return self._timezone
    
    def setTimezone(self, timezone):
        self._timezone = timezone
        
    timezone = property(getTimezone, setTimezone)
    
    def applyId(self, calendarNode):
        tag = "{%s}id" % (Calendar._ATOM_SCHEMA_URI,)
        node = calendarNode.findall(tag)
        self.id = node[0].text
    
    def applyPublished(self, calendarNode):
        tag = "{%s}published" % (Calendar._ATOM_SCHEMA_URI,)
        node = calendarNode.findall(tag)
        self.published = node[0].text
    
    def applyUpdated(self, calendarNode):
        tag = "{%s}updated" % (Calendar._ATOM_SCHEMA_URI,)
        node = calendarNode.findall(tag)
        self.updated = node[0].text
    
    def applyTitle(self, calendarNode):
        tag = "{%s}title" % (Calendar._ATOM_SCHEMA_URI,)
        node = calendarNode.findall(tag)
        self.title = node[0].text
    
    def applyLinks(self, calendarNode):
        tag = "{%s}link" % (Calendar._ATOM_SCHEMA_URI,)
        nodes = calendarNode.findall(tag)
        
        for node in nodes:
            if node.attrib.get("rel") == "self":
                self.linkSelf = node.attrib.get("href")
            elif node.attrib.get("rel") == "alternate":
                self.linkAlternate = node.attrib.get("href")
    
    def applyTimeZone(self, calendarNode):
        tag = "{%s}timezone" % (Calendar._GOOGLE_CALENDAR_SCHEMA_URI,)
        node = calendarNode.findall(tag)
        self.timezone = node[0].text

    def __str__(self):
        if self.title:
            return "%s: '%s'" % (str(self.__class__), self.title)
        else:
            return str(self.__class__)
    __repr__ = __str__